package org.fujene.structure;

import org.fujene.constants.Constants;

public class TimeRangeTuple{
	public static final byte RANGE_GREATER = SearchRangeTuple.RANGE_GREATER;
	public static final byte RANGE_LESS = SearchRangeTuple.RANGE_LESSOREQUAL;
	public static final byte RANGE_EQUAL = SearchRangeTuple.RANGE_EQUAL;
	public static final byte RANGE_NOTEQUAL = SearchRangeTuple.RANGE_NOTEQUAL;
	public static final byte RANGE_RANGE = SearchRangeTuple.RANGE_RANGE;
	public static final byte RANGE_NOTRANGE = SearchRangeTuple.RANGE_NOTRANGE;
	public static final byte RANGE_DONTCARE = SearchRangeTuple.RANGE_DONTCARE;

	private long rangeFrom, rangeTo;
	private boolean equalToFrom;

	public static TimeRangeTuple greaterThan(long value){
		TimeRangeTuple newTuple = new TimeRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = Long.MAX_VALUE;
		newTuple.equalToFrom = true;
		return newTuple;
	}

	public static TimeRangeTuple lessThan(long value){
		TimeRangeTuple newTuple = new TimeRangeTuple();
		newTuple.rangeFrom = Long.MIN_VALUE;
		newTuple.rangeTo = value;
		newTuple.equalToFrom = true;
		return newTuple;
	}

	public static TimeRangeTuple equalValue(long value){
		TimeRangeTuple newTuple = new TimeRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = value;
		newTuple.equalToFrom = true;
		return newTuple;
	}

	public static TimeRangeTuple notEqualValue(long value){
		TimeRangeTuple newTuple = new TimeRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = value;
		newTuple.equalToFrom = false;
		return newTuple;
	}

	public static TimeRangeTuple range(long valueFrom, long valueTo){
		TimeRangeTuple newTuple = new TimeRangeTuple();
		newTuple.rangeFrom = valueFrom;
		newTuple.rangeTo = valueTo;
		newTuple.equalToFrom = true;
		return newTuple;
	}

	public static TimeRangeTuple notRange(long valueFrom, long valueTo){
		TimeRangeTuple newTuple = new TimeRangeTuple();
		newTuple.rangeFrom = valueFrom;
		newTuple.rangeTo = valueTo;
		newTuple.equalToFrom = false;
		return newTuple;
	}

	public static TimeRangeTuple dontCare(){
		TimeRangeTuple newTuple = new TimeRangeTuple();
		newTuple.rangeFrom = Long.MIN_VALUE;
		newTuple.rangeTo = Long.MAX_VALUE;
		newTuple.equalToFrom = true;
		return newTuple;
	}
	
	public static TimeRangeTuple fromMessage(long rangeFrom, long rangeTo, boolean equalToFrom) {
		TimeRangeTuple newTuple = new TimeRangeTuple();
		newTuple.rangeFrom = rangeFrom;
		newTuple.rangeTo = rangeTo;
		newTuple.equalToFrom = equalToFrom;
		return newTuple;
	}

	public boolean isRangeByID(long ID){
		ID >>= Constants.ID_SHIFT;
		if(equalToFrom){
			if(rangeFrom == rangeTo && rangeFrom == ID) return true;
			if(rangeFrom <= ID && ID <= rangeTo) return true;
		}else{
			if(rangeFrom == rangeTo && rangeFrom != ID) return true;
			if(ID <= rangeFrom && rangeTo <= ID) return true;
		}
		return false;
	}
	
	public long rangeFrom() {
		return rangeFrom;
	}
	
	public long rangeTo() {
		return rangeTo;
	}
	
	public boolean equalToFrom() {
		return equalToFrom;
	}
}
